import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.*;

public class AddSubLista {
	public Leitor leitor = new Leitor();
	public JTextField textField= new JTextField(20);
	public JButton salvar = new JButton("Salvar");		
	JFrame frame = new JFrame("Add SubLista");
	GridBagConstraints c = new GridBagConstraints(); 
	GridBagLayout grid = new GridBagLayout();
	int i=0;
	Salvar save = new Salvar();
	public AddSubLista(){
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
		frame.setBounds(30, 30, 300, 300);
		textField.setText(""); //para limpar o texto da caixa
		Container content = frame.getContentPane(); // Get the content pane
		content.setLayout(grid);// Set the container layout mgr
	    			//cria os botoes
		salvar.addActionListener(save);
		c.gridx = 2;
	    c.gridy = 0;
		content.add(salvar,c);
		c.gridx = 0;
	    c.gridy = 0;
		content.add(textField,c);
		frame.pack();
		frame.setVisible(true);
	}
	
	private class Salvar implements ActionListener{
		public void actionPerformed(ActionEvent e) {
		    try {
				FileWriter writer = new FileWriter(Biblioteca.arquivo + ".txt",true);
				writer.write(">" + textField.getText() + "\n"); //o > serve para marcar q eh um SubLista
				writer.close();
				
			} catch (IOException e1) {
				e1.printStackTrace();
			} 
		    Biblioteca.data.clear();
		    for(int i=0;leitor.ler("",Biblioteca.arquivo + ".txt")[i]!=null;i++)
		    	Biblioteca.data.add(leitor.ler("",Biblioteca.arquivo + ".txt")[i]);
		    Biblioteca.list.setListData(Biblioteca.data.toArray());
		    frame.dispose();
		    return;
		}
		
	}
}
 